package ua.com.infopulse.db;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.location.Location;
import android.location.LocationManager;

import java.util.ArrayList;
import java.util.List;

import ua.com.infopulse.spesial_data.LocationData;
import ua.com.infopulse.spesial_data.StringIntCouple;

/**
 * @author Andrew.Nazymko
 */

/**
 * Manage work with  <code>DBHelper.TABLE_TRACKING_DATA</code>  table
 */
public class DataTrackManager {
    private static SQLiteDatabase _database;
    @SuppressWarnings("unused") //will be used in future ;)
	private static String TAG = DataTrackManager.class.getSimpleName();

    DataTrackManager(SQLiteDatabase database) {
        _database = database;
    }

    public void addCheckIN(Location location, String description) {
        ContentValues values = new ContentValues();
        if (location == null) {
            location = new Location(LocationManager.NETWORK_PROVIDER);
            location.setLatitude(Math.random() * 360 - 180);
            location.setLongitude(Math.random() * 180 - 90);

            description = "Cant get location. Random location point generated";
//            throw new IllegalArgumentException("Location location = [null]");
        }
        _database.beginTransaction();
        try {
            values.put(DBHelper.TABLE_TRACKING_DATA_DATE, System.currentTimeMillis());
            values.put(DBHelper.TABLE_TRACKING_DATA_DESCRIPTION, description);
            values.put(DBHelper.TABLE_TRACKING_DATA_LATITUDE, location.getLatitude());
            values.put(DBHelper.TABLE_TRACKING_DATA_LONGITUDE, location.getLongitude());
            values.put(DBHelper.TABLE_TRACKING_DATA_IS_ACCURACY_AVAILABLE, location.hasAccuracy() ? 1 : 0);  /* sqlite  do not support boolean*/
            values.put(DBHelper.TABLE_TRACKING_DATA_ACCURACY, location.getAccuracy());

            _database.insert(DBHelper.TABLE_TRACKING_DATA, null, values);
            _database.setTransactionSuccessful();

        } finally {
            _database.endTransaction();
        }
    }

    /**
     * get check in list <br/>
     * description (short note) and db record id
     *
     * @see StringIntCouple
     */
    public List<StringIntCouple> getCheckInList() {
        List<StringIntCouple> check_in_list = new ArrayList<StringIntCouple>();
        Cursor cursor = getCheckInCursor();
        if (cursor != null) {
            try {
                while (cursor.moveToNext()) {
                    int id = cursor.getInt(cursor.getColumnIndex(DBHelper.TABLE_TRACKING_DATA_ID));
                    String description = cursor.getString(cursor.getColumnIndex(DBHelper.TABLE_TRACKING_DATA_DESCRIPTION));
                    check_in_list.add(new StringIntCouple(description, id));
                }
            } finally {
                cursor.close();
            }
        }

        return check_in_list;
    }

    /**
     * Get full  <code>DBHelper.TABLE_TRACKING_DATA</code> cursor
     */
    public Cursor getCheckInCursor() {
        String selector = "select * from " +DBHelper.TABLE_TRACKING_DATA;
        return _database.rawQuery(selector, null);
    }
     /**
      * Return wrapped longitude,latitude, time and description
      * @see LocationData
      * */
    public LocationData getLocationForId(int id) {
        String selector = "select * from " + DBHelper.TABLE_TRACKING_DATA + " where " + DBHelper.TABLE_TRACKING_DATA_ID + "=" + id;
        Cursor cursor = _database.rawQuery(selector, null);
        LocationData locationData = new LocationData();
        try {
            if (cursor.moveToFirst()) {
                locationData.setDescription(cursor.getString(cursor.getColumnIndex(DBHelper.TABLE_TRACKING_DATA_DESCRIPTION)));
                locationData.setLatitude(cursor.getDouble(cursor.getColumnIndex(DBHelper.TABLE_TRACKING_DATA_LATITUDE)));
                locationData.setLongitude(cursor.getDouble(cursor.getColumnIndex(DBHelper.TABLE_TRACKING_DATA_LONGITUDE)));
                locationData.setTime(cursor.getLong(cursor.getColumnIndex(DBHelper.TABLE_TRACKING_DATA_DATE)));

            }
        } finally {
            cursor.close();
        }
        return locationData;

    }

    public void deletePoint(int tag_id) {
        String deleteQuery = "delete from " + DBHelper.TABLE_TRACKING_DATA + " where " + DBHelper.TABLE_TRACKING_DATA_ID + "=" + tag_id;
        _database.execSQL(deleteQuery);
    }
}

